package com.eipv.multidatasource.component.task;

import com.eipv.multidatasource.entity.UspcServiceinfoComp;
import com.eipv.multidatasource.mapper.mysql.MysqlUspcServiceinfoCompMapper;
import com.eipv.multidatasource.mapper.oracle.OracleUspcServiceinfoCompMapper;
import com.eipv.multidatasource.util.SpringUtils;
import com.github.pagehelper.PageHelper;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicLong;

public class ReadAndWriteDbTask implements Callable {
    private int pageNum;

    private int pageSize;

    private AtomicLong total;

    private AtomicLong start;

    private AtomicLong end;


    public ReadAndWriteDbTask(int pageNum, int pageSize, AtomicLong total, AtomicLong start, AtomicLong end) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
        this.start = start;
        this.end = end;
    }

    @Override
    public Object call() throws Exception {
        MysqlUspcServiceinfoCompMapper mysqlUspcServiceinfoCompMapper = SpringUtils.getBean(MysqlUspcServiceinfoCompMapper.class);

        OracleUspcServiceinfoCompMapper oracleUspcServiceinfoCompMapper = SpringUtils.getBean(OracleUspcServiceinfoCompMapper.class);

        PageHelper.startPage(pageNum, pageSize, true);
        List<UspcServiceinfoComp> list = oracleUspcServiceinfoCompMapper.selectList(null);


        int inserted = mysqlUspcServiceinfoCompMapper.insertInBatch(list);
        end.set(System.currentTimeMillis());
        System.err.println("已处理数据量:" + total.addAndGet(inserted) + "\t,已耗时:" + (end.get() - start.get()) + "(ms)");
        return inserted;
    }
}
